package com.esunrising.esungsc.service.impl;

import com.esunrising.esungsc.dao.FIGLAccDocEntryDao;
import com.esunrising.esungsc.entity.FIGLAccDocEntry;
import com.esunrising.esungsc.service.FIGLAccDocEntryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class FIGLAccDocEntryServiceImpl extends BaseServiceImpl<FIGLAccDocEntryDao, FIGLAccDocEntry> implements FIGLAccDocEntryService {

    @Autowired
    private FIGLAccDocEntryDao figlAccDocEntryDao;

    @Override
    public void syncSave(FIGLAccDocEntry accDocEntry) {
        // 如果存在，则先删除
        Optional<FIGLAccDocEntry> byId = figlAccDocEntryDao.findById(accDocEntry.getID());
        byId.ifPresent( doc -> {
            figlAccDocEntryDao.deleteById(doc.getID());
        });
        // 保存数据
        super.save(accDocEntry);
    }

    /**
     * GSC账务凭证分录数组存入数据库，其中ID重复的会覆盖
     * @param accDocEntryList GSC账务凭证分录数组
     */
    @Override
    public void syncAccDocEntryList(List<FIGLAccDocEntry> accDocEntryList){
        for (FIGLAccDocEntry accDocEntry : accDocEntryList) {
            syncSave(accDocEntry);
        }
    }
}
